$(function() {
	total = document.documentElement.clientHeight;
	colHeight = total - 50 - document.getElementById("rec-body").offsetTop;
	document.getElementById("rec-body").style.height = colHeight + "px";
});

function sendSpeech() {
	var txt = htmlEncode($("#txt").val());
	$("#txt").val("");
	$(".rec-body").append("<div class=\"usr-rep rep\">" + txt + "</div>");
	if (equals("清屏", txt)) {
		$(".rec-body").empty();
		$(".rec-body").append("<div class=\"bot-rep rep\">聊天记录已清空！</div>");
		return false;
	}
	scrollTop();
	if (txt != "") {
		var botName = $('#botName').text();
		$('#botName').text(botName + "(输入中...)");
		$.ajax({
			url : "./",
			type : "POST",
			data : {
				"server": getQueryString("server"),
				"txt" : txt
			},
			dataType : "JSON",
			success : function(e) {
				if (equals(e.code, 1)) {
					$(".rec-body").append(
							"<div class=\"bot-rep rep\">" + e.text + "</div>");
					$('#botName').text(botName);
					scrollTop();
					botName, txt, e = null;
					return false;
				}
			},
			error : function() {
				alert("服务器异常，请稍后重试！");
				$('#botName').text(botName);
				return false;
			}
		});
	}
	return false;
}

function scrollTop() {
	var rsTop = $(".rec-body").scrollTop() + 1800;
	return $(".rec-body").animate({
		scrollTop : rsTop
	}, 800);
}

function htmlEncode(str) {
	return $("<span/>").text(str).html();
}

function htmlDecode(str) {
	return $("<span/>").html(str).text();
}

function equals(str1, str2) {
	return str1 == str2 ? true : false;
}

function toJson(str) {
	return eval('(' + str + ')');
}

function getQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) return unescape(r[2]);
	return null;
}